package com.woniuxy.kaer.service.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.kaer.commons.jwtUtil.JwtUtil;
import com.woniuxy.kaer.commons.util.PageInfo;
import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.service.config.WebSocketServer;
import com.woniuxy.kaer.service.dto.*;
import com.woniuxy.kaer.service.entity.ServiceOrder;
import com.woniuxy.kaer.service.from.MakeServiceOrderFrom;
import com.woniuxy.kaer.service.from.RateOrderFrom;
import com.woniuxy.kaer.service.from.ReplyCommentFrom;
import com.woniuxy.kaer.service.from.SelectOrderFrom;
import com.woniuxy.kaer.service.param.MakeServiceOrderParam;
import com.woniuxy.kaer.service.param.RateOrderParam;
import com.woniuxy.kaer.service.param.ReplyCommentParam;
import com.woniuxy.kaer.service.param.SelectOrderParam;
import com.woniuxy.kaer.service.service.ServiceOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 程诗洋
 * @since 2025年02月17日
 */
@RestController
@RequestMapping("/serviceOrder")
@Api(tags = "服务订单类")
public class ServiceOrderController {
    @Resource
    private ServiceOrderService serviceOrderService;
    @Resource
    private JwtUtil jwtUtil;
    @ApiOperation("预约服务接口")
    @PostMapping("/makeServiceOrder")
    public Result makeServiceOrder(@RequestBody MakeServiceOrderFrom from,@RequestHeader("token")String token)throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        MakeServiceOrderParam makeServiceOrderParam = BeanUtil.toBean(from, MakeServiceOrderParam.class);
        makeServiceOrderParam.setUserId(userId);
        OrderMessageDto orderMessageDto =serviceOrderService.makeServiceOrder(makeServiceOrderParam);

        return Result.succeed("预约服务成功",orderMessageDto);
    }
    //
    @ApiOperation("接单")
    @PutMapping("/takeServiceOrder")
    public Result takeServiceOrder(String serviceOrderId,@RequestHeader("token")String token)throws Exception{
        System.out.println(token);
        Integer founderId = jwtUtil.getFounderId(token);
        serviceOrderService.takeServiceOrder(serviceOrderId,founderId);
        return Result.succeed("接单成功",null);
    }
    //确定服务项目之后，用户确认订单，不能在对服务订单项目进行更改
    @ApiOperation("用户确认订单")
    @PutMapping("/userConfirmOrder")
    public Result userConfirmOrder(String serviceOrderId,@RequestHeader("token")String token)throws Exception{
        Integer userId= jwtUtil.getUserId(token);
        serviceOrderService.userConfirmOrder(serviceOrderId,userId);
        return Result.succeed("用户确认订单成功",null);
    }
  //发起支付
  @ApiOperation("发起支付")
  @PostMapping("/pay")
  public Result pay(String serviceOrderId,@RequestHeader("token")String token)throws Exception{
      Integer userId = jwtUtil.getUserId(token);
      String from = serviceOrderService.pay(serviceOrderId,userId);
      return Result.succeed("发起支付成功",from);
  }


    @ApiOperation("支付宝回调")
    @PostMapping("/notify")
    public String payNotify(HttpServletRequest request)throws Exception{
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();){
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        serviceOrderService.handleOrder(params);
        return "SUCCESS";
    }
    //对订单的评分，（，同时计算商家评分，异步操作），评价表的记录详细信息，日记表的记录
    @ApiOperation("对订单的评分")
    @PutMapping("/rateOrder")
    public Result rateOrder(@RequestBody RateOrderFrom from,@RequestHeader("token")String token)throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        RateOrderParam param = BeanUtil.toBean(from, RateOrderParam.class);
        param.setUserId(userId);
        serviceOrderService.rateOrder(param);
        return Result.succeed("评价订单成功",null);
    }

    //订单已过期，商家手动取消订单
//    public Result tenantsCancelTimeoutOrder(@RequestParam("serviceOrderId")String serviceOrderId,@RequestHeader("token")String token)throws Exception{
//        Integer founderId = jwtUtil.getFounderId(token);
//        serviceOrderService.OrderTimeout(serviceOrderId,founderId);
//        return Result.succeed("订单超时，商户手动更改订单，订单状态为超时",null);
//    }
    //通过Id查询订单
    @ApiOperation("通过Id查询订单")
    @GetMapping("/getServiceOrderById")
    public Result<OrderInfoDto> getServiceOrderById(String serviceOrderId)throws Exception{
        OrderInfoDto orderInfoDto = serviceOrderService.getServiceOrderById(serviceOrderId);
        return Result.succeed("查询订单成功",orderInfoDto);
    }
    //查询所有需要待接单的预约订单
    @ApiOperation("查询所有需要待接单的预约订单")
    @GetMapping("/getAllServiceOrder")
    public Result<List<OrderInfoDto>> getAllServiceOrder(Integer shopId,@RequestHeader("token")String token)throws Exception{
        //Integer founderId = jwtUtil.getFounderId(token);
        List<OrderInfoDto> orderInfoDtos = serviceOrderService.getAllServiceOrder(shopId);
        return Result.succeed("查询所有待接单的预约订单成功",orderInfoDtos);
    }
    //分页查询该店铺所有订单
    @ApiOperation("分页查询该店铺所有订单")
    @GetMapping("/getAllServiceOrderByPage")
    public Result<PageInfo> getAllServiceOrderByPage(SelectOrderFrom from)throws Exception{
        SelectOrderParam selectOrderParam = BeanUtil.toBean(from, SelectOrderParam.class);
        PageInfo OrderInfoPageInfo  =serviceOrderService.getAllServiceOrderByPage(selectOrderParam);
        return Result.succeed("分页查询该店铺所有订单成功",OrderInfoPageInfo);
    }
    //查询出正在处理的订单
    @ApiOperation("查询出正在处理的订单")
    @GetMapping("/getAllServiceOrderByProcessing")
    public Result<List<OrderInfoDto>> getAllServiceOrderByProcessing(Integer shopId,@RequestHeader("token")String token)throws Exception{
        Integer founderId = jwtUtil.getFounderId(token);
        List<OrderInfoDto> orderInfoDtos = serviceOrderService.getAllServiceOrderByProcessing(shopId,founderId);
        return Result.succeed("查询出正在处理的订单成功",orderInfoDtos);
    }
    //查询未支付的订单
    @ApiOperation("查询需要支付的订单")
    @GetMapping("/getAllServiceOrderByUnpaid")
    public Result<List<OrderInfoDto>> getAllServiceOrderByUnpaid(@RequestHeader("token")String token)throws Exception{
        System.out.println(token);
        Integer userId = jwtUtil.getUserId(token);
        List<OrderInfoDto> orderInfoDtos = serviceOrderService.getAllServiceOrderByUnpaid(userId);
        return Result.succeed("查询未支付的订单成功",orderInfoDtos);
    }
    //查询业务流程完成的和已支付的订单
    @ApiOperation("查询业务流程完成的和已支付的订单")
    @GetMapping("/getAllServiceOrderByFinished")
    public Result<List<OrderInfoDto>> getAllServiceOrderByFinished(@RequestHeader("token")String token)throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        List<OrderInfoDto> orderInfoDtos = serviceOrderService.getAllServiceOrderByFinished(userId);
        return Result.succeed("查询业务流程完成的和已支付的订单成功",orderInfoDtos);
    }
   // 用户确认完成订单
   @ApiOperation("用户确认订单完成")
    @PutMapping("/userConfirmOrderFinish")
    public Result userConfirmOrderFinish(String serviceOrderId,@RequestHeader("token")String token)throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        serviceOrderService.userConfirmOrderFinish(serviceOrderId,userId);
        return Result.succeed("用户确认完结订单成功",null);
    }
    //查询当天的完结订单数，预约订单数，销售额
    @ApiOperation("查询当天的完结订单数，预约订单数，销售额")
    @GetMapping("/getTodayOrderInfo")
    public Result<TodayOrderInfoDto> getTodayOrderInfo(@RequestHeader("token")String token)throws Exception{
        Integer founderId = jwtUtil.getFounderId(token);
        TodayOrderInfoDto todayOrderInfoDto = serviceOrderService.getTodayOrderInfo(founderId);
        return Result.succeed("查询当天的完结订单数，预约订单数，销售额成功",todayOrderInfoDto);
    }
    //查询3个月的预约订单数，销售总额，完成订单数
    @ApiOperation("查询3个月的预约订单数，销售总额，完成订单数")
    @GetMapping("/getThreeMonthOrderInfo")
    public Result<ThreeMonthMerchantStatisticsDto> getThreeMonthOrderInfo(@RequestHeader("token")String token)throws Exception{
        Integer founderId = jwtUtil.getFounderId(token);
        ThreeMonthMerchantStatisticsDto threeMonthOrderInfoDto = serviceOrderService.getThreeMonthOrderInfo(founderId);
        return Result.succeed("查询3个月的预约订单数，销售总额，完成订单数成功",threeMonthOrderInfoDto);
    }
    //查询当前订单的所有服务项目和消耗品
    @ApiOperation("查询当前订单的所有服务项目和消耗品")
    @GetMapping("/getAllServiceAndConsumablesByServiceOrderId")
    public Result<List<ServiceAndServiceRealDetailDto>> getAllServiceAndConsumablesByServiceOrderId(String serviceOrderId)throws Exception{
        List<ServiceAndServiceRealDetailDto> serviceAndConsumablesDtos = serviceOrderService.getAllServiceAndConsumablesByServiceOrderId(serviceOrderId);
        return Result.succeed("查询当前订单的所有服务项目和消耗品成功",serviceAndConsumablesDtos);
    }
    //商家对用户评论的回复
    @ApiOperation("商家对用户评论的回复")
    @PutMapping("/replyComment")
    public Result replyComment(@RequestBody ReplyCommentFrom from, @RequestHeader("token")String token)throws Exception{
        Integer founderId = jwtUtil.getFounderId(token);
        ReplyCommentParam replyCommentParam = BeanUtil.toBean(from, ReplyCommentParam.class);
        replyCommentParam.setFounderId(founderId);
        serviceOrderService.replyComment(replyCommentParam);
        return Result.succeed("商家对用户评论的回复成功",null);
    }


}

